import UnitX from "../../entity/UnitX";
import CommonLoopEvent from "../../event/eventdef/CommonLoopEvent";

/**  */
export default class TenaManager{
    
    /**单位id  单位当前丢失的韧性值 */
    private static unitLostTenaValMap:Map<number,number>=new Map()

    /**单位从气竭到恢复要花的时间 */
    private static unitResumeSecondsMap:Map<number,number>=new Map()

    private static resumeTennaEvent:CommonLoopEvent

    private static resumeWeakUnitEvent:CommonLoopEvent

    private static RESUME_TENNA_INTERVAL_VAL=2   //x秒一次丢失韧性恢复

    private static ONCE_RESUME_TENNAVAL=2  //每次恢复的韧性值


    /**
     * 减少单位的韧性 
     * 并且自动开启韧性恢复 以及 判断是否韧性扣减为0 进入气竭
     * 
     * @param unitx 
     * @param tenaVal 
     * @return 是否造成了气竭
     */
    public static reduceUnitTena(unitx:UnitX,tenaVal:number):boolean{
        let makeTargetWeak=false
        if(tenaVal<=0){
            return makeTargetWeak
        }
        let v=this.unitLostTenaValMap.get(unitx.unitXId)
        if(v==undefined){
            v=tenaVal
        }else{
            v=v+tenaVal
        }
        //当前韧性丢失值已经大于单位韧性最大值   并且单位不在气竭状态中
        if(v>(unitx.attrs?.tena || 0) && this.unitResumeSecondsMap.get(unitx.unitXId)==undefined){
            //单位气竭 10秒指恢复时间
            this.unitResumeSecondsMap.set(unitx.unitXId,4)
            this.unitLostTenaValMap.delete(unitx.unitXId)
            console.log("单位气竭")
            makeTargetWeak=true
        }else{
            this.unitLostTenaValMap.set(unitx.unitXId,v)
            console.log(unitx.unitXId,"单位当前丢失韧性：",v)
        }
        //韧性削弱单位缓慢恢复丢失的韧性
        this.resumeLostTena()
        return makeTargetWeak
    }

    /**
     * 任何韧性削弱都需要开启恢复韧性执行器
     * @returns 
     */
    private static resumeLostTena(){

        //开启单位韧性恢复
        if(this.resumeTennaEvent==undefined){
            this.resumeTennaEvent=new CommonLoopEvent('SECONDS',TenaManager.RESUME_TENNA_INTERVAL_VAL,()=>{
                if(this.unitLostTenaValMap.size==0){
                    this.resumeTennaEvent.pauseEvent()
                    return
                }
                this.unitLostTenaValMap.forEach((value,key)=>{
                    if(value<=TenaManager.ONCE_RESUME_TENNAVAL){
                        this.unitLostTenaValMap.delete(key)
                        console.log(key,"单位韧性无损!!!")
                        //单位气竭状态恢复
                    }else{
                        this.unitLostTenaValMap.set(key,value-TenaManager.ONCE_RESUME_TENNAVAL)
                        console.log(key,"单位韧性丢失值为:",(value-TenaManager.ONCE_RESUME_TENNAVAL))
                    }
                })
            })
            this.resumeTennaEvent.beAddedAndExec()
        }else{
            //两个恢复事件同步初始化的 直接跳出  少一个判断
            this.resumeTennaEvent.resumePause()
            this.resumeWeakUnitEvent.resumePause()
            return 
        }

        //开启单位气竭状态恢复
        if(this.resumeWeakUnitEvent==undefined){
            this.resumeWeakUnitEvent=new CommonLoopEvent('SECONDS',1,()=>{
                if(this.unitResumeSecondsMap.size==0){
                    this.resumeWeakUnitEvent.pauseEvent()
                    return
                }
                this.unitResumeSecondsMap.forEach((value,key)=>{
                    if(value<=1){
                        this.unitResumeSecondsMap.delete(key)
                        console.log(key,"单位气竭状态恢复!! ")
                        //单位气竭状态恢复
                    }else{
                        this.unitResumeSecondsMap.set(key,value-1)
                        console.log(key,"单位距离气竭状态恢复还有:",(value-1),"秒")
                    }
                })
            })
            this.resumeWeakUnitEvent.beAddedAndExec()
        }

    }

  
}  